package com.ruoyi.mdkt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.mdkt.entity.ScoreLog;
import com.ruoyi.mdkt.mapper.ScoreLogMapper;
import com.ruoyi.mdkt.service.ScoreLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gwl
 * @since 2022-10-22
 */
@Service
public class ScoreLogServiceImpl extends ServiceImpl<ScoreLogMapper, ScoreLog> implements ScoreLogService {

    @Autowired
    private ScoreLogMapper scoreLogMapper;

    @Override
    public boolean saveScoreLog(Integer score, String info, Long userId, Long courseId) {
        LambdaQueryWrapper<ScoreLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ScoreLog::getUserId, userId).eq(ScoreLog::getCourseId, courseId);
        ScoreLog scoreLog = scoreLogMapper.selectOne(lambdaQueryWrapper);
        if (scoreLog != null) {
            ScoreLog scoreLog1 = new ScoreLog();
            scoreLog1.setCourseId(scoreLog.getCourseId());
            scoreLog1.setUserId(scoreLog.getUserId());
            scoreLog1.setOlderScore(scoreLog.getNewScore());
            scoreLog1.setScoreChange(score);
            scoreLog1.setNewScore(score + scoreLog.getNewScore());
            scoreLog1.setScoreInfo(info);
            if (save(scoreLog1)) {
                return true;
            } else {
                return false;
            }
        } else {
            ScoreLog scoreLog1 = new ScoreLog();
            scoreLog1.setCourseId(courseId);
            scoreLog1.setUserId(userId);
            scoreLog1.setOlderScore(0);
            scoreLog1.setScoreChange(score);
            scoreLog1.setNewScore(score);
            scoreLog1.setScoreInfo(info);
            if (save(scoreLog1)) {
                return true;
            } else {
                return false;
            }
        }
    }
}
